<?php
namespace app\admin\controller;
class User extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!='') $map[] = ['and','username|name|phone','like',"%{$d['keyword']}%"];
            if($d['min_time']!='') $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            $rs = $this->db->query("select * from user $w[0] order by id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'status' => function($v){return c("type.status.{$v}");},
                    'is_rz' => function($v){return c("type.is_rz.{$v}");},
                    'sex' => function($v){return c("type.sex.{$v}");},
                    'level' => function($v){return c("type.user_level.{$v}");},
                    'is_team' => function($v){return c("type.user_is_team.{$v}");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }
    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'username'=>'require',
            ];
            $msg = [
                'username.require' => '用户名称不能为空',
            ];
            check($rule,$msg,$d);
            $user = $this->db->find('user',$d['id']);

            //修改了推荐人
            if($user['s_id']!=$d['s_id']){
              $prent_user = $this->db->find('user', $d['s_id']);
              if(in_array($user['id'], explode(",",$prent_user['s_tree']))){
                err('下级会员不能成为上级会员');
              }else{
                $data = [
                  'id' => $user['id'],
                  's_id' => $prent_user['id'],
                  's_tree' => $prent_user['s_tree'].$prent_user['id'].",",
                ];
                $data['s_level'] = count(explode(',', $data['s_tree']))-2;
                $this->db->update('user', $data);
                $old_tree = $user['s_tree'].$user['id'].",";
                $new_tree = $data['s_tree'].$user['id'].",";
                $num = count(explode(',',$new_tree))-count(explode(',',$old_tree));
                $this->db->query("update user set s_tree=REPLACE(s_tree,'$old_tree','$new_tree'), s_level=s_level+{$num} where s_tree like '{$old_tree}%'");
              }
            }else{
              $this->db->update("user",$d);
            }
            m('Attachment','xcom')->imgSure('user',$d['id'],$d);
            m('Log','xcom')->record($this->userinfo['id'],'编辑了用户资料:'.json_encode($d,JSON_UNESCAPED_UNICODE),'admin_user');
            cookie('js_msg','修改成功');
            cookie('js_function','preload');
            return suc();
        }
        $id = $this->get["id"];
        $sql = "select * from user where id=?";
        $info = $this->db->query($sql,[$id],1);
        $this->fetch(["info" => $info]);
    }

    //变动资金
    public function editMoney(){
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'id'         =>  'require',
                'money'         =>  'require|float',
                'note'          =>  'require',
            ];
            $msg = [
                'money.require' =>'金额不能为空',
                'money.float'   =>'金额格式不对',
                'note.require'  =>'请填写备注信息',
            ];
            check($rule,$msg,$d);
            $rs = m('Orderumoney','c')->doit($d['id'], $this->userinfo['id'], 'money', $d['money'], $d['note']);
            cookie('js_function','preload');
            if($rs){
                cookie('js_msg','变动资金成功');
                suc();
            }else{
                err(m('OrderUmoney','c')->getError());
            }
        }
        $id=$this->get('id');
        //模板赋值
        $info = $this->db->find('user',$id);
        $this->fetch(['info'=>$info]);
    }
}
